import json
import matplotlib.pyplot as plt
import pandas as pd
import os

# 读取CNN评价指标
with open('cnn_metrics.json', 'r') as f:
    metrics = json.load(f)

# 指标名称
columns = ['Accuracy', 'Precision', 'Recall', 'F1-score']

# 构建DataFrame
metrics_df = pd.DataFrame([metrics], columns=columns, index=["CNN"])

# 绘制表格
fig, ax = plt.subplots(figsize=(8, 2))
ax.axis('off')
table = ax.table(cellText=metrics_df.values,
                 colLabels=metrics_df.columns,
                 rowLabels=metrics_df.index,
                 loc='center',
                 cellLoc='center')

# 调整字体大小
table.auto_set_font_size(False)
table.set_fontsize(10)  # 调整为更小的字体

# 自适应列宽
table.auto_set_column_width([0, 1, 2, 3])  # 自适应所有列

# 调整行高和列宽比例
table.scale(1.2, 1.5)

# 保存图片
save_path = os.path.join('实验结果可视化', 'CNN评价指标表格.png')
plt.savefig(save_path, bbox_inches='tight', dpi=300)
print(f'CNN评价指标表格已保存到: {save_path}')
plt.show()
